﻿using BackEnd.Common;
using BusinessLayer.RegInfo;
using System;
using System.Web.Mvc;
using BackEnd.Models;

namespace BackEnd.Controllers
{
    public class AdminPrivilegesController : Controller
    {
        //
        // GET: /AdminPrivileges/Manage
        [CustomAuthorize(Action = Common.Constants.AdminActions.CreatePrivileges)]
        public ActionResult Manage()
        {
            var lstUserGroups = new AdminUserGroupBL().GetAll();
            var model = new PrivilegeModel();
            model.ListUserGroups = lstUserGroups;
            var lstUsers = new AdminUserBL().GetAll();
            model.ListUsers = lstUsers;
            var lstActions = new AdminActionBL().GetAll();
            model.ListActions = lstActions;
            var lstMenu = new AdminMenuBL().GetAll();
            model.ListMenu = lstMenu;

            return View(model);
        }

        [HttpPost]
        [CustomAuthorize(Action = Common.Constants.AdminActions.CreatePrivileges)]
        public ActionResult Manage(PrivilegeModel model)
        {
            if (ModelState.IsValid)
            {
                var action = model.Action;

                var groupBL = new AdminUserGroupBL();
                //ViewData["ErrorMessage"] = model.SelectedUserIDs.ToString() + "/" + model.SelectedActionIDs.ToString() + model.SelectedMenuIDs.ToString();
                for (int i = 0; i < model.SelectedUserGroupIDs.Length; i++)
                {
                    var group = groupBL.GetByUserGroupID(model.SelectedUserGroupIDs[i]);
                    if (group != null)
                    {
                        
                        //Delete all old privileges
                        groupBL.DeleteAllPrivilegesOfUserGroup(group.UserGroupID);
                        
                        if (model.SelectedUserIDs != null && model.SelectedUserIDs.Length > 0)
                        {
                            for (int u = 0; u < model.SelectedUserIDs.Length; u++)
                            {
                                groupBL.AddUserToUserGroup(model.SelectedUserIDs[u], model.SelectedUserGroupIDs[i]);
                            }

                        }
                        if (model.SelectedActionIDs != null && model.SelectedActionIDs.Length > 0)
                        {
                            for (int a = 0; a < model.SelectedActionIDs.Length; a++)
                            {
                                groupBL.AddActionToUserGroup(model.SelectedActionIDs[a], model.SelectedUserGroupIDs[i]);
                            }

                        }
                        if (!String.IsNullOrEmpty(model.SelMenuIDs))
                        {
                            string[] menuIDArr = model.SelMenuIDs.Split(';');
                            for (int m = 0; m < menuIDArr.Length; m++)
                            {
                                int menuid = -1;
                                Int32.TryParse(menuIDArr[m], out menuid);
                                if (menuid > 0)
                                {
                                    groupBL.AddMenuToUserGroup(menuid, model.SelectedUserGroupIDs[i]);
                                }
                            }

                        }
                    }
                }
                //Ok
                return RedirectToAction("Index", "Home");
            }
            var lstUserGroups = new AdminUserGroupBL().GetAll();
            model.ListUserGroups = lstUserGroups;
            var lstUsers = new AdminUserBL().GetAll();
            model.ListUsers = lstUsers;
            var lstActions = new AdminActionBL().GetAll();
            model.ListActions = lstActions;
            var lstMenu = new AdminMenuBL().GetAll();
            model.ListMenu = lstMenu;
            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "Users, actions, menu or gorups is incorrect.");
            return View(model);
        }

    }
}
